import os
from os import path

from selenium.webdriver import Ie, Chrome, ChromeOptions, IeOptions

from apps.logger_config import logger


class DriverType:
    IE = 'ie'
    CHROME = 'chrome'


class CreateDriver:
    @classmethod
    def create(cls, driver_type=DriverType.IE):
        logger.info('打开浏览器 %s', driver_type)
        base_dir = os.path.dirname(os.path.abspath(__file__))
        if driver_type == DriverType.IE:
            options = IeOptions()
            options.add_argument("ignoreZoomSetting")
            driver = Ie(executable_path=path.join(base_dir, 'exec/IEDriverServer.exe'))
            driver.maximize_window()
            return driver
        if driver_type == DriverType.CHROME:
            options = ChromeOptions()
            # headless 不启用浏览器界面，在linux下必须启用这个选项
            # , "--auto-open-devtools-for-tabs", "--headless"
            args = ["--start-maximized", "--enable-print-preview"]
            # 关闭图片加载
            # prefs = {'profile.managed_default_content_settings.images': 2}
            for arg in args:
                options.add_argument(arg)
            # options.add_experimental_option("prefs", prefs)
            return Chrome(options=options,
                          executable_path=path.join(base_dir, 'exec/chromedriver.exe'))
